//
//  EnterLiveViewController.m
//  sklive
//
//  Created by Macx on 2017/5/5.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "EnterLiveViewController.h"

#import "TCLiveListModel.h"
#import "HUDHelper.h"
#import "TCLiveListModel.h"
#import "TCPlayViewController_LinkMic.h"
#import "UIColor+MLPFlatColors.h"
#import "BlocksKit.h"

@interface EnterLiveViewController () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) NSDictionary *liveData;

@end

@implementation EnterLiveViewController{
    BOOL             _hasEnterplayVC;
}

- (void)viewDidAppear:(BOOL)animated {
    _hasEnterplayVC = NO;
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    [self GETLivesViewers];
}
- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self buildUI];
    
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:bgImageView];
    bgImageView.image = [UIImage imageNamed:@"live_enter_bg"];
    
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:returnBtn];
    returnBtn.frame = CGRectMake(0, 20, SW(10), 40);
    [returnBtn setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [returnBtn addTarget:self action:@selector(returnBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *liveTitle = [[UILabel alloc] init];
    [self.view addSubview:liveTitle];
    liveTitle.text = @"LiveTitle";
    liveTitle.textAlignment = NSTextAlignmentCenter;
    liveTitle.textColor = [UIColor whiteColor];
    liveTitle.font = kUHSystemFontWithSize(17);
    CGSize liveTitleSize = [LZCTools textSizeWithText:liveTitle.text font:liveTitle.font];
    liveTitle.frame = CGRectMake(self.view.center.x-liveTitleSize.width/2, self.view.center.y-liveTitleSize.height, liveTitleSize.width, liveTitleSize.height);
//    [liveTitle addActionWithblock:^{
//        LZCAlertView *alert = [[LZCAlertView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//        [alert initShow:[[NSUserDefaults standardUserDefaults] valueForKey:USERINFO]];
//        [self.view addSubview:alert];
//    }];
    
    UILabel *liveNum = [[UILabel alloc] init];
    [self.view addSubview:liveNum];
    liveNum.text = @"正在直播中\n12345人在看";
    liveNum.numberOfLines = 2;
    liveNum.textAlignment = NSTextAlignmentCenter;
    liveNum.textColor = [UIColor whiteColor];
    liveNum.font = kUHSystemFontWithSize(13);
    CGSize liveNumSize = [LZCTools textSizeWithText:liveNum.text font:liveNum.font];
//    liveNum.frame = CGRectMake(0, watchBtn.frame.origin.y-20-liveNumSize.height*2, SCREEN_WIDTH, liveNumSize.height*2);
    liveNum.frame = CGRectMake(0, self.view.bounds.size.height-liveNumSize.height*2-SW(20), SCREEN_WIDTH, liveNumSize.height*2);
    
    UIButton *watchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:watchBtn];
    [watchBtn setTitle:@"观看直播" forState:UIControlStateNormal];
    [watchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    CGFloat btnHeight = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(14)].height+SW(20);
//    watchBtn.frame = CGRectMake(W(10), self.view.bounds.size.height-btnHeight-W(20), SCREEN_WIDTH-W(5), btnHeight);
    watchBtn.frame = CGRectMake(SW(10), liveNum.y-20-btnHeight, SCREEN_WIDTH-SW(5), btnHeight);
    watchBtn.backgroundColor = RGB(244, 124, 153);
    watchBtn.layer.cornerRadius = btnHeight/2;
    watchBtn.layer.masksToBounds = YES;
    [watchBtn addTarget:self action:@selector(watchLiveAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}

#pragma mark ==========响应方法==========
- (void)watchLiveAction:(id)sender {
    
    TCLiveInfo *info = [[TCLiveInfo alloc] init];
    info.userinfo = [[TCLiveUserInfo alloc] init];

//    info.userid = @"87cc605bce2c8bea492dc1f8cb73c731"; // 9254
    info.userid = [self.liveData[@"anchor"] valueForKey:@"username"];
    info.groupid = self.liveData[@"chatroom_id"];
    info.viewercount = [self.liveData[@"viewer_num"] intValue];
    info.likecount = [self.liveData[@"like_num"] intValue];
    info.title = self.liveData[@"title"];
    info.playurl = self.liveData[@"pull_url"][0];
    info.timestamp = (int)self.liveData[@"start_time"];
    info.userinfo.nickname = [self.liveData[@"anchor"] valueForKey:@"nickname"];
    info.userinfo.headpic = [self.liveData[@"anchor"] valueForKey:@"avatar"];
    info.userinfo.frontcover = self.liveData[@"cover"];
//    info.type // 直播点播回放的类型
//    info.fileid
//    info.userinfo.location
//    info.userinfo.frontcoverImage
    
    // MARK: 打开播放界面
    if (_playVC == nil) {
        _playVC = [[TCPlayViewController_LinkMic alloc] initWithPlayInfo:info videoIsReady:^{
            if (!_hasEnterplayVC) {
                [self.navigationController pushViewController:_playVC animated:YES];
                _hasEnterplayVC = YES;
            }
        }];
    }
    
    [self performSelector:@selector(enterPlayVC:) withObject:_playVC afterDelay:0.5];
    
}
-(void)enterPlayVC:(NSObject *)obj{
    if (!_hasEnterplayVC) {
        [self.navigationController pushViewController:_playVC animated:YES];
        _hasEnterplayVC = YES;
    }
}

/**
 获取最新直播
 */
- (void)GETLivesViewers {
    
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    
//    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
//    
//    NSString *url = [APILives stringByAppendingFormat:@"/%@/viewers", self.liveId];
//    
//    NSLog(@"进入直播间 URL = %@", url);
//    
//    [LZCTools networkRequest:session];
//    
//    [session POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        NSLog(@"进入直播间 成功 = %@", responseObject);
//        
//        self.liveData = responseObject;
//        
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        NSLog(@"进入直播间 失败 = %@", error);
//        [self presentViewController:[LZCTools AFNErrorAlert:error] animated:YES completion:nil];
//    }];
    
}



- (void)returnBtnAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
